package cn.xsirl.websocket.util;

/**
 * @version V1.0
 * @Title:
 * @ClassName: $PackageName$.$ClassName$.java
 * @Description:
 * @author: 谢明珑
 * @date: 2021/5/27$ 20:38$
 */
public class BaseUtil {
    public static byte[] short2byte(short s) {
        byte[] bytes = new byte[2];
        bytes[0] = (byte) ((s >> 8) & 0xff);
        bytes[1] = (byte) (s & 0xff);
        return bytes;
    }
    
    public static short byte2short(byte[] bytes) {
        short n = 0;
        for (int i = 0; i < bytes.length && i < 2; i++) {
            n |= (bytes[i] & 0xff) << ((bytes.length - 1 - i) * 8);
        }
    
        return n;
    }
    
    public static byte[] int2byte(int i) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) ((i >> 24) & 0xff);
        bytes[1] = (byte) ((i >> 16) & 0xff);
        bytes[2] = (byte) ((i >> 8) & 0xff);
        bytes[3] = (byte) (i & 0xff);
        return bytes;
    }
    
    public static int byte2int(byte[] bytes) {
        int n = 0;
        for (int i = 0; i < bytes.length && i < 4; i++) {
            n |= (bytes[i] & 0xff) << ((bytes.length - 1 - i) * 8);
        }
        
        return n;
    }
    
    public static void main(String[] args) {
        byte[] a = { 49 , 49};
    
//        System.out.println(ByteBuffer.wrap(a).getInt());
        System.out.println(byte2int(a));
    }
}
